package controllers;

import java.util.List;
import java.util.ArrayList;

import play.Logger;
import play.mvc.*;

import com.fasterxml.jackson.databind.JsonNode; 
import com.sun.glass.ui.Application;

import views.html.*;
import models.*;
import models.msgs.Info;

public class Application extends Controller {
	
	public static int numberOfPlayers = 2;
	public static int actualPlayers = 0;
	public static Game game;
    /**
     * Display the home page.
     */
    public static Result index() {
        return ok(index.render());
    }
  
    /**
     * Display the chat room.
     */
    public static Result chatRoom(String username) {
        if(username == null || username.trim().equals("")) {
            flash("error", "Please choose a valid username.");
            return redirect(routes.Application.index());
        }
        return ok(chatRoom.render(username));
    }

    public static Result chatRoomJs(String username) {
        return ok(views.js.chatRoom.render(username));
    }
    
    /**
     * Handle the chat websocket.
     */
    public static WebSocket<JsonNode> chat(final String username) {
    	actualPlayers++;
    	if (actualPlayers <= numberOfPlayers) {
    		
    		
    		return new WebSocket<JsonNode>() {
                
                // Called when the Websocket Handshake is done.
                public void onReady(WebSocket.In<JsonNode> in, WebSocket.Out<JsonNode> out){
                    
                	Table.notifyAll(new Info("I've got a WebSocket", username));
                	
                    // Join the chat room.
                    try {                	
                    	Table.join(username, in, out);
                    	
                    	
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                   

                }
            };
    	} else {
    		actualPlayers--;
    		return null;
    	}
    }
  
}
